package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cake.db.MySqlDb;
import cake.bean.Manager;
//����Ա��ݿ���ʶ���
public class ManagerDao {
	private MySqlDb db=new MySqlDb();
	//ͨ��id��ȡ����Ա����
	public Manager getManager(long id)
	{
		Manager manager=null;
		String sql="select * from manager where id="+id;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				manager=new Manager();
				manager.setUsername(rs.getString("managername"));
				manager.setPassword(rs.getString("password"));
				manager.setId(rs.getLong("id"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return manager;
	}
	//ͨ�����Ա��ƺ������ȡ����Ա����
	public Manager getManager(String managername,String password)
	{
		Manager manager=null;
		String sql="select * from manager where username='"+managername+"' and password='"+password+"'";
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				manager=new Manager();
				manager.setUsername(rs.getString("username"));
				manager.setPassword(rs.getString("password"));
				manager.setId(rs.getLong("id"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return manager;
	}
	//��ҳ��ȡ����Ա�б?��pageSizeΪ0ʱ��ʾ����ҳ
	public List<Manager> getManagers(int currPage,int pageSize,String where)
	{
		List<Manager> managers=new ArrayList<Manager>();
		String sql="select * from manager ";
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				Manager manager=new Manager();
				manager.setUsername(rs.getString("managername"));
				manager.setPassword(rs.getString("password"));
				manager.setId(rs.getLong("id"));
				
				managers.add(manager);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return managers;
	}
	//��������Ա
	public boolean addManager(Manager manager)
	{
		String sql="Insert into Manager(managername,password) values('"+manager.getUsername()+"','"+manager.getPassword()+"')";
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
		
	}
	//�޸Ĺ���Ա
	public boolean updateManager(Manager manager)
	{
		Manager oldManager=getManager(manager.getId());
		
		if(oldManager==null)
		{
			return false;
		}
		
		String sql="Update Manager set password='"+manager.getPassword()+"' where id="+manager.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	//ɾ�����Ա
	public  boolean deleteManager(Manager manager)
	{
		Manager oldManager=getManager(manager.getId());
		
		if(oldManager==null)
		{
			return false;
		}
		
		String sql="Delete from Manager where id="+manager.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
}
